package net.core.base.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;

public class CommonDAOImpl extends SqlMapClientDaoSupport implements CommonDAO {

	public List<HashMap> getPageList(String sql, int pageSize, int pn) {
		Map map = new HashMap();
		map.put("sql", sql);
		//queryForList(name,参数,跳过记录数,返回的最大记录数)
		List list = getSqlMapClientTemplate().queryForList("Common.page_DataList", map, ((pn - 1) * pageSize),
				pageSize);
		return list;
	}

	public int getPageNum(String sql) {
		Map map = new HashMap();
		map.put("sql", sql);
		int row = (Integer) getSqlMapClientTemplate().queryForObject("Common.page_DataNum", map);
		return row;
	}

	public List<HashMap> getDataList(String sql) {
		Map map = new HashMap();
		map.put("sql", sql);
		List list= null;
		try{
			list = getSqlMapClientTemplate().queryForList("Common.select_sql", map);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return list;
	}

	public HashMap getInforByRecId(String tableName, String recId) {
		Map map = new HashMap();
		map.put("tableName", tableName);
		map.put("recId", recId);
		HashMap record = (HashMap) getSqlMapClientTemplate().queryForObject("Common.inforByRecId_sql", map);
		return record;
	}

	public int insertSql(String sql) {
		getSqlMapClientTemplate().insert("Common.insert_sql", sql);
		return 1;
	}

	public int updateSql(String sql) {
		int row = getSqlMapClientTemplate().update("Common.update_sql", sql);
		return row;
	}

	public int deleteDataReal(String sql) {
		int row = getSqlMapClientTemplate().delete("Common.delete_sql", sql);
		return row;
	}
	
	public int deleteDataReal(String tableName, String rec[]) {
		Map map = new HashMap();
		map.put("tableName", tableName);
		map.put("rec", rec);
		int row = getSqlMapClientTemplate().delete("Common.deleteMoreSql", map);
		return row;
	}

	public int deleteData(String tableName, String rec, String user) {
		Map map = new HashMap();
		map.put("tableName", tableName);
		map.put("rec", rec);
		map.put("user", user);
		int row = getSqlMapClientTemplate().update("Common.update_Data", map);
		return row;
	}
	
	public int deleteData(String tableName, String rec[], String user) {
		Map map = new HashMap();
		map.put("tableName", tableName);
		map.put("rec", rec);
		map.put("user", user);
		int row = getSqlMapClientTemplate().update("Common.updateMoreData", map);
		return row;
	}

	public int runSql(String sql) {
		int row = 0;
		if (sql.toUpperCase().indexOf("UPDATE") != -1) {
			row = getSqlMapClientTemplate().update("Common.update_sql", sql);
		} else if (sql.toUpperCase().indexOf("DELETE") != -1) {
			row = getSqlMapClientTemplate().delete("Common.delete_sql", sql);
		} else {
			getSqlMapClientTemplate().queryForObject("Common.exeSqlString", sql);
		}
		return row;
	}
	
	public int batchInsert(List list, Log logger) {
		int result = 0;
		SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
		try {
			if (list != null) {
				// 开始事务
				sqlMapClient.startTransaction();
				// 设置当前connection 自动提交为false
				sqlMapClient.getCurrentConnection().setAutoCommit(false);

				for (int i = 0, n = list.size(); i < n; i++) {
					sqlMapClient.insert("Common.insert_sql", list.get(i));
				}
				// 提交事务，
				sqlMapClient.getCurrentConnection().commit();
				result = list.size();
			}
		} catch (Exception e) {
			logger.error("batchInsert出错!!", e);// e.printStackTrace();
			logger.error("batchInsert error: batchInsert, parameterObject [" + list + "].  Cause: " + e.getMessage());
			result = -1;
		} finally {
			try {
				// 结束事务
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public int batchUpdate(List list, Log logger) {
		int result = 0;
		SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
		try {
			if (list != null) {
				// 开始事务
				sqlMapClient.startTransaction();
				// 设置当前connection 自动提交为false
				sqlMapClient.getCurrentConnection().setAutoCommit(false);

				for (int i = 0, n = list.size(); i < n; i++) {
					sqlMapClient.update("Common.update_sql", list.get(i));
				}
				// 提交事务，
				sqlMapClient.getCurrentConnection().commit();
				result = list.size();
			}
		} catch (Exception e) {
			logger.error("batchUpdate出错!!", e);
			logger.error("batchInsert error: batchInsert, parameterObject [" + list + "].  Cause: " + e.getMessage());
			result = -1;
		} finally {
			try {
				// 结束事务
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public int batchRunSql(List list, Log logger) {
		int result = 0;
		SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
		try {
			if (list != null) {
				// 开始事务
				sqlMapClient.startTransaction();
				// 设置当前connection 自动提交为false
				sqlMapClient.getCurrentConnection().setAutoCommit(false);

				for (int i = 0, n = list.size(); i < n; i++) {
					String sql = (String) list.get(i);
					if (sql.toUpperCase().trim().startsWith("UPDATE")) {
						sqlMapClient.update("Common.update_sql", sql);
					} else if (sql.toUpperCase().trim().startsWith("DELETE")) {
						sqlMapClient.delete("Common.delete_sql", sql);
					} else if (sql.toUpperCase().trim().startsWith("INSERT")) {
						sqlMapClient.insert("Common.insert_sql", sql);
					}
				}
				// 提交事务，
				sqlMapClient.getCurrentConnection().commit();
				result = list.size();
			}
		} catch (Exception e) {
			logger.error("batchUpdate出错!!", e);
			logger.error("batchInsert error: batchRunSql, parameterObject [" + list + "].  Cause: " + e.getMessage());
			result = -1;
		} finally {
			try {
				// 结束事务
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	@Override
	public long getSeqNum(String seqName) {
		return ((Long) getSqlMapClientTemplate().queryForObject("Common.getSeqNum", seqName)).longValue();
	}
}
